package com.gitee.xmhzzz.component.swagger.plugin;

import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import io.swagger.annotations.ApiModelProperty;
import springfox.documentation.schema.Annotations;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;

import java.util.Optional;

/**
  * @ClassName
  * @Description
  * @Author wzq
  * @Date 2023/12/18 10:23
  * @Version 1.0
  */
public class CustomizeModelPropertyBuilderPlugin implements ModelPropertyBuilderPlugin {
    @Override
    public void apply(ModelPropertyContext context) {
        Optional<BeanPropertyDefinition> beanPropertyDefinition = context.getBeanPropertyDefinition();
        if (beanPropertyDefinition.isPresent()) {
            Optional<ApiModelProperty> annotationOpt = Annotations.findPropertyAnnotation(beanPropertyDefinition.get(), ApiModelProperty.class);
            if (annotationOpt.isPresent()) {
                String example = annotationOpt.get().example();
                if (example != null) {
                    context.getBuilder().example("");
                }
            }
        }
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }
}
